package controllers

import (
	"fmt"
	"pfl/db"
	"pfl/models"

	"github.com/astaxie/beego"
)

type RegisterController struct {
	beego.Controller
}

func (this *RegisterController) Get() {
	this.TplName = "register.html"
}
func (this *RegisterController) Post() {
	username := this.GetString("username")
	password := this.GetString("password")
	if username == "" {
		this.Ctx.WriteString("请输入用户名")
		return
	}
	if password == "" {
		this.Ctx.WriteString("请输入密码")
		return
	}
	fmt.Println(username, password)
	user := models.Prevuser{
		Username: username,
		Password: password,
	}
	sess := db.Mysql.NewSession()
	defer sess.Close()
	sess.Begin()
	rows, err := sess.InsertOne(&user)

	if rows != 1 || err != nil {
		sess.Rollback()
		this.Ctx.WriteString("注册失败，可能用户名已经存在，请重试")
		return
	}
	sess.Commit()
	this.Ctx.WriteString("注册成功")

}
